package gui;

import player.MyPlayer;
import player.MyPlayerThread;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PlayerGui extends MyFrame{

    private String songList[]={"./music/KEY.L刘聪,Kafe.Hu - 经济舱.mp3",
            "./music/特曼-青鸟（中文版）（翻自陈乐一）.mp3",
            "./music/许嵩-玫瑰花的葬礼.mp3",
            };

    //面板定义
    private JPanel playPanel;
    private static BlankPanel showPlayPanel;

    //定义按钮等相关组件
    ImageButton bntStart,bntStop,bntNext,bntPause,bntPrev;

    //定义播放器
    MyPlayer player;
    MyPlayerThread playerThread;

    //监听器定义
    PlayOperatorListener playOperatorListener;

    public PlayerGui(){
        //初始化图形界面相关组件
        init();

        //非线程化音乐播放器
        player=new MyPlayer(songList);
        //线程化音乐播放器
        playerThread=new MyPlayerThread(songList);

        //添加监听事件
        addActionListener();
    }
    //播放面板初始化
    public void init(){
        //设置主窗口布局
        getContentPane().setLayout(null);

        //面板实例化
        playPanel=new BlankPanel();
        playPanel.setSize(1000,150);
        showPlayPanel=new BlankPanel();

        //面板设置
        playPanel.setLayout(new BorderLayout());
        showPlayPanel.setLayout(new FlowLayout());

        //组件实例化
        bntStart=new ImageButton("./drawable/icon/play.png");
        bntNext=new ImageButton("./drawable/icon/next.png");
        bntPrev=new ImageButton("./drawable/icon/prev.png");
        bntPause=new ImageButton("./drawable/icon/pause.png");




        //添加组件
        showPlayPanel.add(bntPrev);
        showPlayPanel.add(bntPause);
        showPlayPanel.add(bntStart);
        showPlayPanel.add(bntNext);
        //默认设置暂停按钮不可见，播放按钮可见
        bntPause.setVisible(false);


        //监听器实例化
        playOperatorListener=new PlayOperatorListener();

        //添加面板
        getContentPane().add(playPanel);
        playPanel.setLocation(0,450);
        playPanel.add(showPlayPanel,BorderLayout.CENTER);


    }

    public void addActionListener(){
        //播放组件监听事件
        bntStart.addActionListener(playOperatorListener);
    }
    class PlayOperatorListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getSource()==bntStart){
                //player.playList();
                playerThread.start();
                bntStart.setVisible(false);
                bntPause.setVisible(true);
            }
            else if(e.getSource()==bntPause){

            }
        }
    }
}

